﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.DAL;
using Gift.Model;

namespace WebApp.admin.news
{
    public partial class friendLink : BaseAdmin
    {
        // Fields

        tbFrendLink oFrendLink = new tbFrendLink();
        readonly DALFrendLink theFrendLink = new DALFrendLink();


        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oFrendLink = theFrendLink.GetModel(int.Parse(Request.QueryString["id"]));
                }
                this.oFrendLink.linkName = this.txtLinkName.Value;
                this.oFrendLink.linkUrl = this.txtLinkUrl.Value;
             
                if (this.fileLinkLogo.PostedFile.FileName != "")
                {
                    this.oFrendLink.linkLogo = "/cn/frend_link_logo/" + DateTime.Now.ToString("yyyyMMddHHssmmffffff") + Path.GetExtension(this.fileLinkLogo.PostedFile.FileName);
                }
                this.oFrendLink.sortOrder = Convert.ToInt32(this.txtSortOrder.Value);
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oFrendLink.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    tbFrendLink frendLinkById = this.theFrendLink.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                    if (this.theFrendLink.Update(this.oFrendLink))
                    {
                        if (this.fileLinkLogo.PostedFile.FileName != "")
                        {
                            if (frendLinkById != null)
                            {
                                File.Delete(base.Server.MapPath(frendLinkById.linkLogo));
                            }
                            this.fileLinkLogo.PostedFile.SaveAs(base.Server.MapPath(this.oFrendLink.linkLogo));
                        }
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"friendLinkList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                    }
                }
                else if (this.theFrendLink.Add(this.oFrendLink) > 0)
                {
                    if (this.fileLinkLogo.PostedFile.FileName != "")
                    {
                        this.fileLinkLogo.PostedFile.SaveAs(base.Server.MapPath(this.oFrendLink.linkLogo));
                    }
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"friendLinkList.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }

        private void FrendLinkBind()
        {
            try
            {
                tbFrendLink frendLinkById = this.theFrendLink.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (frendLinkById != null)
                {
                    this.txtLinkName.Value = frendLinkById.linkName;
                    this.txtLinkUrl.Value = frendLinkById.linkUrl;
                    if (frendLinkById.linkLogo != "")
                    {
                        this.imgLogo.Src = frendLinkById.linkLogo;
                        this.imgLogo.Style.Add("display", "block");
                    }
                    else
                    {
                        this.imgLogo.Style.Add("display", "none");
                    }
                    this.txtSortOrder.Value = frendLinkById.sortOrder.ToString();
                }
            }
            catch
            {
            }
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if (base.Request.QueryString["ot"] == null)
                {
                    return builder;
                }
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    builder.Append("编辑链接");
                    return builder;
                }
                builder.Append("添加链接");
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.FriendLink);
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.FrendLinkBind();
                }
                else if (base.Request.QueryString["ot"] == "ad")
                {
                    this.imgLogo.Style.Add("display", "none");
                }
            }
        }

    }
}